package com.juku2024.juku.analysis

import android.content.Context
import android.text.TextUtils
import com.juku2024.juku.db.entity.SourceEntity
import com.juku2024.juku.tools.AnalysisUtils
import com.juku2024.juku.tools.Constants
import com.juku2024.juku.tools.LogC
import com.juku2024.juku.tools.NetworkUtils
import io.reactivex.disposables.Disposable

abstract class BaseHttpParse {
    var startTime = 0L
    var disposable: Disposable? = null


    open fun release(){
        disposable?.dispose()
    }



    open fun proceed(){}
    open fun cancel(){}


    private fun getPlayTime(playTime: Long): String {
        return when (playTime) {
            in 0..10 -> {
                playTime.toString() + "s"
            }
            else -> {
                "10+s"
            }
        }
    }

    // 统计解析次数
    private fun analysisParseTimes(sourceName: String?){
        AnalysisUtils.analysis(Constants.EVENT_SOURCE, Constants.KEY_SOURCE_TIMES, sourceName ?: "unknown")
    }


    open fun parseVideo(context: Context, url: String, source: SourceEntity, callback: OnParseVideoListener) {
        if (TextUtils.isEmpty(source.url)) {
            callback.onFail(source.sourceName)
            return
        }
        if (NetworkUtils.isWifiProxy()) {
            callback.onFail(source.sourceName)
            return
        }
        analysisParseTimes(source.sourceName)
        startTime = System.currentTimeMillis()
        disposable?.dispose()
        LogC.e("parse -> ${source.sourceName}解析准备...")
    }


    fun analysisParseDuration(sourceName: String?){
        val endTime = System.currentTimeMillis() - startTime
        val duration = getPlayTime(endTime / 1000)
        LogC.e("parse -> ${sourceName}源-> http解析成功...${duration}")

        AnalysisUtils.analysis(
            Constants.EVENT_SOURCE,
            Constants.KEY_SOURCE_DURATION,
            sourceName + "_" + duration
        )
    }
}